package com.axdac.mixbind.sql.adapter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


import com.axdac.mixbind.sql.db.ORMUtil;
import com.axdac.mixbind.sql.db.TableCreate;
import com.axdac.mixbind.sql.db.TableUpdate;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by Administrator on 2018/4/4 0004.
 */

public class AndroidDBAdapter  extends SQLiteOpenHelper implements IDBAdapter {
    private Object tables = null;
    public AndroidDBAdapter(Context context, String name, SQLiteDatabase.CursorFactory factory, int version,Object tables) {
        super(context, name, factory, version);
        this.tables = tables;
    }

    //--------------------------------------------

    @Override
    public void onCreate(SQLiteDatabase db) {
        if(tables!=null){
            Class<?> clazz = tables.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for(Field field:fields){
                field.setAccessible(true);
                if(field.isAnnotationPresent(TableCreate.class)) {
                    TableCreate tableCreate = field.getAnnotation(TableCreate.class);
                    if (tableCreate != null) {
                        try {
                            String sql = field.get(tables).toString();
                            if (sql != null) {
                                Log.d("Query", "execsql :" + sql);
                                db.execSQL(sql);
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(tables!=null){
            Class<?> clazz = tables.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for(Field field:fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(TableUpdate.class)) {
                    TableUpdate tableUpdate = field.getAnnotation(TableUpdate.class);
                    if (!tableUpdate.newVersion().equals(newVersion + "")
                            || !tableUpdate.oldVersion().equals(oldVersion + "")) {
                        continue;
                    }
                    if (tableUpdate != null) {
                        try {
                            String sql = field.get(tables).toString();
                            if (sql != null) {
                                Log.d("Query", "execsql :" + sql);
                                db.execSQL(sql);
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }


    //--------------------------------------------
    @Override
    public List<Object> execSql(String SQL, String entity) {
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.rawQuery(SQL,null);
        return ORMUtil.bindObject(cursor,entity);
    }

    @Override
    public boolean execSql(String SQL) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL(SQL);
        return true;
    }
}
